<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-12-30 16:38:52
 * @LastEditors: Andy
 * @LastEditTime: 2023-03-28 15:56:31
-->
<template>
  <el-form ref="form" :model="ruleForm" :rules="rules" label-position="top">
    <el-form-item label="用户姓名" prop="username">
      <el-input v-model="ruleForm.username" maxlength="30" clearable />
    </el-form-item>
    <el-form-item label="真实姓名" prop="realName">
      <el-input v-model="ruleForm.realName" maxlength="30" clearable />
    </el-form-item>
    <el-form-item label="联系方式" prop="mobile">
      <el-input v-model="ruleForm.mobile" maxlength="50" clearable />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="ruleForm.email" maxlength="50" clearable />
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <x-radio-group :list-data="dictOptions['upms_user.gender']" v-model="ruleForm.gender"></x-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button @click="handleClose"> 取消 </el-button>
      <el-button type="primary" @click="handleSubmit"> 保存 </el-button>
    </el-form-item>
  </el-form>
</template>
<script>
  import { checkEmail, checkUserName, checkMobilePhone } from '@/common/utils/validate.js'
  import { apiPostEditUserBase } from '../api'
  import { EnumApiCode } from '../../../../../../globalEnum'

  export default {
    props: {
      user: {
        type: Object
      },
      dictOptions: {
        type: Object
      }
    },
    data() {
      return {
        ruleForm: {
          username: '',
          realName: '',
          mobile: '',
          email: '',
          gender: ''
        },
        rules: {
          username: [
            { required: true, message: this.$t('system.user.userPlaceholder'), trigger: 'blur' },
            { min: 1, max: 20, message: this.$t('system.user.userLengthPlaceholder'), trigger: 'blur' }
            // { validator: this.validateUserName, trigger: 'blur' }
          ],
          realName: [
            { required: true, message: this.$t('system.user.np'), trigger: 'blur' },
            { min: 1, max: 20, message: this.$t('system.user.userLengthPlaceholder'), trigger: 'blur' },
            { validator: this.validateName, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: this.$t('system.user.enterPassword'), trigger: 'blur' },
            { validator: this.validatePhone, trigger: 'blur' }
          ],
          email: [
            { required: true, message: this.$t('system.user.emailTxt'), trigger: 'blur' },
            { validator: this.validateEmail, trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      user: {
        handler(val) {
          this.ruleForm = Object.assign(this.ruleForm, val)
          this.ruleForm.gender += ''
        },
        immediate: true,
        deep: true
      }
    },
    methods: {
      validateUserName(rule, value, callback) {
        if (value && value !== '' && !checkUserName(value)) {
          callback(new Error(this.$t('system.user.userNameTips')))
        } else {
          callback()
        }
      },
      validateName(rule, value, callback) {
        value = value.replace(/\s*/g, '')
        if (value.length === 0) {
          callback(new Error(this.$t('system.user.np')))
        } else {
          callback()
        }
      },
      validateEmail(rule, value, callback) {
        if (value && value !== '' && !checkEmail(value)) {
          callback(new Error(this.$t('system.user.EmailAddresses')))
        } else {
          callback()
        }
      },
      validatePhone(rule, value, callback) {
        if (value && value !== '' && !checkMobilePhone(value)) {
          callback(new Error(this.$t('system.user.PleasePhoneRight')))
        } else {
          callback()
        }
      },
      handleSubmit() {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.ruleForm
            }
            const { code, msg } = await apiPostEditUserBase(param)
            if (code === EnumApiCode.Success) {
              this.$refs.form.resetFields()
              this.$message.success(this.$t('system.user.editSuccess'))
              this.$emit('handleSuccess')
            } else {
              this.$message.error(msg)
            }
          } else {
            return false
          }
        })
      },
      handleClose() {
        this.$refs.form.resetFields()
        this.ruleForm = Object.assign(this.ruleForm, this.user)
        this.ruleForm.gender += ''
      }
    }
  }
</script>
